package com.neusoft.elm.view;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.impl.BusinessDaoImpl;
import com.neusoft.elm.po.Business;

import java.util.List;
import java.util.Scanner;

public class BusinessViewImpl implements BusinessView{

    Scanner sc = new Scanner(System.in);
    @Override
    public void listBusinessAll(){
        BusinessDao dao = new BusinessDaoImpl();
        List<Business> list = dao.listBusiness(null,null);
        System.out.println("商家编号\t\t商家名称\t\t商家地址\t\t商家介绍\t\t起送费\t\t配送费");
        for(Business b : list){
            System.out.println(b.getBusinessId()+"\t"+b.getBusinessName()+"\t"+b.getBusinessAddress()+"\t"+b.getBusinessExplain()+"\t"+b.getStarPrice()+"\t"+b.getDeliveryPrice());
        }
    }

    @Override
    public void listBusiness(){
        String businessName = "";
        String businessAddress = "";

        String inputStr = "";
        System.out.println("是否需要输入商家名称关键词(y/n): ");
        inputStr = sc.next();
        if(inputStr.equals("y")){
            System.out.println("请输入商家名称关键词: ");
            businessName = sc.next();
        }

        inputStr = "";
        System.out.println("是否需要输入商家地址关键词(y/n): ");
        inputStr = sc.next();
        if(inputStr.equals("y")){
            System.out.println("请输入商家地址关键词: ");
            businessAddress = sc.next();
        }

        BusinessDao dao = new BusinessDaoImpl();
        List<Business> list = dao.listBusiness(businessName,businessAddress);
        System.out.println("商家编号\t\t商家名称\t\t商家地址\t\t商家介绍\t\t起送费\t\t配送费");
        for(Business b : list){
            System.out.println(b.getBusinessId()+"\t"+b.getBusinessName()+"\t"+b.getBusinessAddress()+"\t"+b.getBusinessExplain()+"\t"+b.getStarPrice()+"\t"+b.getDeliveryPrice());
        }
    }
    @Override
    public void saveBusiness(){
        System.out.println("请输入商家名称: ");
        String businessName = sc.next();
        BusinessDao dao = new BusinessDaoImpl();
        int businessId = dao.saveBusiness(businessName);
        if(businessId > 0){
            System.out.println("新建商家成功!!!");
            System.out.println("商家编号为: "+businessId);
        }else{
            System.out.println("新建商家失败!!!");
        }
    }

    @Override
    public void removeBusiness(){
        System.out.println("请输入商家编号: ");
        int businessId = sc.nextInt();

        BusinessDao dao = new BusinessDaoImpl();
        System.out.println("确认删除吗?(y/n): ");
        if(sc.next().equals("y")){
            int result = dao.removeBusiness(businessId);
            if(result == 1){
                System.out.println("删除商家成功!!!");
            }else{
                System.out.println("删除商家失败!!!");
            }
        }
    }


    @Override
    public Business login(){
        System.out.println("请输入商家编号: ");
        int businessId = sc.nextInt();
        System.out.println("请输入密码: ");
        String password = sc.next();

        BusinessDao dao = new BusinessDaoImpl();
        return dao.getBusinessByIdByPass(businessId,password);

    }

    @Override
    public void showBusiness(Integer businessId){
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);
        System.out.println(business);
    }

    @Override
    public void editBusiness(Integer businessId){
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);
        System.out.println(business);

        String inputStr = "";
        System.out.println("是否修改商家名称(y/n): ");
        inputStr = sc.next();
        if(inputStr.equals("y")){
            System.out.println("请输入新的商家名称: ");
            business.setBusinessName(sc.next());
        }

        System.out.println("是否修改商家地址: ");
        inputStr = sc.next();
        if(inputStr.equals("y")){
            System.out.println("请输入新的商家地址: ");
            business.setBusinessAddress(sc.next());
        }

        System.out.println("是否修改商家介绍: ");
        inputStr = sc.next();
        if(inputStr.equals("y")){
            System.out.println("请输入新的商家介绍: ");
            business.setBusinessExplain(sc.next());
        }

        System.out.println("是否修改起送费: ");
        inputStr = sc.next();
        if(inputStr.equals("y")){
            System.out.println("请输入新的起送费: ");
            business.setStarPrice(sc.nextDouble());
        }

        System.out.println("是否修改配送费: ");
        inputStr = sc.next();
        if(inputStr.equals("y")){
            System.out.println("请输入新的配送费: ");
            business.setDeliveryPrice(sc.nextDouble());
        }
        int result = dao.updateBusiness(business);
        if(result > 0){
            System.out.println("\n修改商家信息成功!!!\n");
        }else{
            System.out.println("\n修改商家信息失败!!!\n");
        }
    }
    @Override
    public void updateBusinessByPassword(Integer businessId){
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);

        System.out.println("\n请输入旧密码: ");
        String oldPass = sc.next();
        System.out.println("\n请输入新密码: ");
        String password = sc.next();
        System.out.println("\n请再次输入新密码: ");
        String beginPassword = sc.next();

        if(!business.getPassword().equals(oldPass)){
            System.out.println("\n旧密码输入错误!!! ");
        }else if(!password.equals(beginPassword)){
            System.out.println("\n两次输入密码不一致!!!");
        }else{
            int result = dao.updateBusinessByPassword(businessId,password);
            if(result > 0){
                System.out.println("\n修改密码成功!!!");
            }else{
                System.out.println("\n修改密码失败!!!");
            }
        }
    }
}
